package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");
        System.out.println("请输入书名");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.nextLine();
        System.out.println("请输入作者");
        String author=scanner.nextLine();
        System.out.println("请输入类型");
        String type=scanner.nextLine();
        System.out.println("请输入价格");
        int price=scanner.nextInt();
        int UseSize= bookList.getUseSize();
//        通过新创建一个对象来把新书的信息储存起来
        Book newBook=new Book(name,author,price,type);
//        对重复书籍的判断
        for (int i = 0; i < UseSize; i++) {
            Book book=bookList.getBook(i);
            if (book.getName().equals(name)){
                System.out.println("已经有此书，无需重复添加");
                return;
            }
        }
//        指向下一个空的书架位置
        bookList.setUseSize(UseSize+1);
//        添加书籍进入书架
        bookList.SetBooks(newBook,UseSize);
        System.out.println("新增图书成功");
    }
}
